package com.zhiyou100.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.zhiyou100.entity.Dept;
import com.zhiyou100.mapper.DeptMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;

import com.zhiyou100.service.DeptService;

public class DeptServiceImpl implements DeptService {
	@Override
	public List<Dept> getAllDept() {
		System.out.println("DeptServiceImpl 1");
		InputStream resourceAsStream = null;
		try {
			System.out.println("DeptServiceImpl 2");
			resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("DeptServiceImpl 3");
		SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

		SqlSessionFactory sf = sqlSessionFactoryBuilder.build(resourceAsStream);

		SqlSession sqlSession = sf.openSession();

		DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
		System.out.println("DeptServiceImpl 4");
		List<Dept> allDept = mapper.getAllDept();
		sqlSession.close();
		return allDept;
	}





}
